/*
 * $Id: ArtistList.java 562 2008-08-23 20:59:33Z jasta00 $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.five.music.activity;

import org.devtcg.five.music.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ArtistList extends Activity
{
	private static final String TAG = "ArtistList";

	private static final String PROJECTION[] = {
		"_id", "name", "full_name", "photo"
	};

	private ListView mList;

	private Cursor getCursor(String selection, String[] args)
	{
	  return getContentResolver().query(getIntent().getData(), PROJECTION,
		selection, args, "name COLLATE UNICODE ASC");
	}

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.artist_list);

        Intent i = getIntent();
        if (i.getData() == null)
        	i.setData(Uri.parse("content://org.devtcg.five/media/music/artists"));

        if (i.getAction() == null)
        	i.setAction(Intent.ACTION_VIEW);

        Cursor c = getCursor(null, null);

        if (c.getCount() == 0)
        {
        	Toast.makeText(this, "No data, please use Five Sync first",
        	  Toast.LENGTH_LONG).show();
        	finish();
        	return;
        }

        startManagingCursor(c);

		mList = (ListView)findViewById(R.id.artist_list);

		ArtistAdapter adapter = new ArtistAdapter(this,
		  R.layout.artist_list_item, c,
		  new String[] { "photo", "full_name" },
		  new int[] { R.id.artist_photo, R.id.artist_name });
		
		mList.setAdapter(adapter);
        mList.setOnItemClickListener(mClickEvent);
        mList.setTextFilterEnabled(true);
    }

    private OnItemClickListener mClickEvent = new OnItemClickListener()
    {
		public void onItemClick(AdapterView adapter, View v, int pos, long id)
		{
			Intent i = getIntent();

			/* Clicked on a separator row. */
			if (id == -1)
				return;

			Uri chosen = i.getData().buildUpon()
			  .appendPath(String.valueOf(id))
			  .build();
			
			String action = i.getAction();
			
			Log.d(TAG, "uri=" + chosen.toString());
			Log.d(TAG, "action=" + action);

			if (action.equals(Intent.ACTION_PICK) == true ||
			    action.equals(Intent.ACTION_GET_CONTENT) == true)
			{
				setResult(RESULT_OK, (new Intent()).setData(chosen));
				finish();
			}
			else
			{
				startActivity(new Intent(Intent.ACTION_VIEW,
				  chosen, ArtistList.this, AlbumAndSongList.class));
			}
		}
    };

    private class ArtistAdapter extends SimpleCursorAdapter
      implements FilterQueryProvider
    {
		public ArtistAdapter(Context context, int layout, Cursor c,
		  String[] from, int[] to)
		{
			super(context, layout, c, from, to);
			setFilterQueryProvider(this);
		}

		public Cursor runQuery(CharSequence constraint)
		{
			String sel = null;
			String[] args = null;

			Log.d(TAG, "runQuery: " + constraint);

			if (TextUtils.isEmpty(constraint) == false)
			{
				sel = "UPPER(name) GLOB ?";

				String wildcard = constraint.toString().replace(' ', '*');
				args = new String[] { "*" + wildcard.toUpperCase() + "*" };
			}

			return ArtistList.this.getCursor(sel, args);
		}

		@Override
		public void changeCursor(Cursor cursor)
		{
			stopManagingCursor(mCursor);

			super.changeCursor(cursor);

			if (cursor != null)
				startManagingCursor(cursor);
		}
    }
}
